﻿//-----------------------------------------------------------------------
// <copyright file="MessageTypeDetection.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.DomainObjects.MessageTypes
{
    using System;
    using System.Collections.Generic;

    using ChatBox.DomainObjects.XmlFeedObjects;
    using ChatBox.Utility;

    /// <summary>
    /// Class to detect message type from message text
    /// </summary>
    public class MessageTypeDetection
    {
        /// <summary>
        /// prefix text to message type mapping
        /// </summary>
        private readonly Dictionary<string, MessageType> prefixTextToMessageTypeMappings = new Dictionary<string, MessageType>
               {
                   { "/me", MessageType.Me },
                   { "/sing", MessageType.Sing },
                   { "/whisper", MessageType.Whisper },
                   { "/egg", MessageType.Egg },
                   { "/smite", MessageType.Smite },
                   { "/rolls", MessageType.Rolls },
               };

        /// <summary>
        /// Read the text and set the message text type
        /// </summary>
        /// <param name="message">the message to process</param>
        public void SetMessageTextType(Message message)
        {
            if (string.IsNullOrEmpty(message.RawMessageText))
            {
                message.MessageType = MessageType.Plaintext;
                return;
            }

            bool prefixFound = false;

            // look for a known prefix (e.g. "/me") and map to the matching message type
            if (message.RawMessageText.StartsWith("/", StringComparison.OrdinalIgnoreCase))
            {
                foreach (string prefix in this.prefixTextToMessageTypeMappings.Keys)
                {
                    if (message.RawMessageText.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
                    {
                        message.MessageText = message.RawMessageText.TrimPrefix(prefix);

                        message.MessageType = this.prefixTextToMessageTypeMappings[prefix];
                        prefixFound = true;
                        break;
                    }
                }
            }

            if (!prefixFound)
            {
                message.MessageText = message.RawMessageText;
                message.MessageType = MessageType.Plaintext;
            }
        }
    }
}
